package com.mjw.zstu.controller;

import com.mjw.zstu.entity.Company;
import com.mjw.zstu.entity.Message;
import com.mjw.zstu.resultDto.JsonResult;
import com.mjw.zstu.service.CompanyService;
import com.mjw.zstu.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("company")
public class CompanyController {
    @Autowired
    MessageService messageService;
    @Autowired
    CompanyService companyService;
    @RequestMapping("detail")
    public JsonResult detail(int company_id){
        return companyService.company_detail(company_id);
    }
    @RequestMapping("update")
    public JsonResult update(@RequestBody Company company){
        return companyService.update(company);
    }
    @RequestMapping("compare")
    public JsonResult compare(int company_id,String password){
        return companyService.comparePassword(company_id,password);
    }
    @RequestMapping("login")
    public JsonResult login(@RequestBody Company company){
        System.out.println(company);
        Company company1=companyService.login(company);
        System.out.println(company1);
        if(company1!=null){
            company1.setPassword(null);
            return new JsonResult(200,"登录成功",company1);
        }
        return new JsonResult(500,"用户不存在",null);
    }
    @RequestMapping("register")
    public JsonResult register(@RequestBody Company company){
        Company company1=new Company();
        company1.setUsername(company.getUsername());
        Company c=companyService.find(company1);
        if(c!=null){
            return new JsonResult(500,"用户名存在,请重新输入",null);
        }
        int success=companyService.register(company);
        company.setPassword(null);
        if(success==1&&c==null){
            return new JsonResult<Company>(200,"注册成功",company);
        }
        return new JsonResult(500,"用户名存在,请重新输入",null);
    }
    @RequestMapping("list")
    public JsonResult getList(int pageNum,int pageSize){
        return new JsonResult(200,"获取成功",companyService.getCompanyList(pageNum,pageSize));
    }
    @RequestMapping("review")
    public JsonResult review(@RequestParam int id){
        Company company=new Company();
        company.setCompany_id(id);
        company.setIs_review(1);

        int success=companyService.review(company);
        if(success==1){
            Message message=new Message();
            long t=System.currentTimeMillis();
            message.setCreate_time(t);
            message.setMessage("公司通过审核啦！！");
            message.setReceiver(company.getCompany_id());
            message.setIs_user(0);
            message.setIs_read(0);
            messageService.inserts(message);
            return new JsonResult(200,"审核成功",null);
        }
        return new JsonResult(500,"审核失败",null);
    }
    @RequestMapping("refuse")
    public JsonResult refuse(@RequestBody Company company){
        Message message=new Message();
        long t=System.currentTimeMillis();
        message.setCreate_time(t);
        message.setMessage("公司未通过审核。"+"未通过原因："+company.getRefuse_reason());
        message.setReceiver(company.getCompany_id());
        message.setIs_user(0);
        message.setIs_read(0);
        messageService.inserts(message);
        return companyService.refuse(company);
    }
}
